<template>
  <div class="login-body">
    <div class="container">
      <form class="form-signin" action="index.html">
        <div class="form-signin-heading text-center">
          <h1 class="sign-title">胖虎记账本</h1>
          <h2 style="color:#c9d4dd;font-family:'Microsoft YaHei'">Register</h2>
        </div>
        <el-form :model="ruleForm"
                 label-position="right"
                 status-icon
                 :rules="rules"
                 ref="ruleForm"
                 class="login-wrap"
        >
          <!--               class="demo-ruleForm"label-width="100px"-->
          <el-form-item  prop="userName">
            <el-input v-model="ruleForm.userName" autocomplete="off" placeholder="用户名" clearable autofocus></el-input>
          </el-form-item>
          <el-form-item  prop="passWord">
            <el-input type="password" v-model="ruleForm.passWord" placeholder="密码" autocomplete="off" show-password clearable></el-input>
          </el-form-item>
          <el-form-item  prop="checkPassWord">
            <el-input type="password" v-model="ruleForm.checkPassWord" placeholder="确认密码" autocomplete="off" show-password clearable></el-input>
          </el-form-item>
          <el-button type="primary" class="btn btn-lg btn-login btn-block" @click="submitForm('ruleForm')">
                      <i class="el-icon-check"></i>
          </el-button>
          <div class="registration">
              Have a member yet?
              <a class="" href="/login">Login</a>
          </div>
        </el-form>
      </form>
    </div>
    <Footer style="margin: 12% auto;"></Footer>
  </div>
</template>

<script>
import Footer from '@/components/Footer'
export default {
  // eslint-disable-next-line vue/multi-word-component-names
  name: "Register",
  components:{Footer},
  data() {
    var validateUsername = async (rule, value, callback) => {
      if (!value) {
        return callback(new Error('用户名不能为空'));
      }
      //查询用户名是否存在
      const result=await this.$API.default.user.userNameExist(value);
      if(result=="用户名已存在"){
        return callback(new Error('用户名已存在'))
      }
      //需要判断用户名是否重名
      callback();
    };
    var validatePass = (rule, value, callback) => {
      if (!value) {
        callback(new Error('请输入密码'));
      } else {
        if (this.ruleForm.checkPassWord !== '') {
          this.$refs.ruleForm.validateField('checkPass');
        }
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'));
      } else if (value !== this.ruleForm.passWord) {
        callback(new Error('两次输入密码不一致!'));
      } else {
        callback();
      }
    };
    return {
      ruleForm: {
        userName:"",
        passWord: '',
        checkPassWord: '',
      },
      rules: {
        passWord: [
          {validator: validatePass, trigger: 'blur'}
        ],
        checkPassWord: [
          {validator: validatePass2, trigger: 'blur'}
        ],
        userName: [
          {validator: validateUsername, trigger: 'blur'}
        ]
      }
    };
  },
  methods: {
    submitForm(formName) {
      const userInfo={
        name:"",
        password:""
      };
      userInfo.name=this.ruleForm.userName;
      userInfo.password=this.ruleForm.passWord;
      // await this.$API.default.user.registerUser(userInfo);
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          //发起注册请求
          const result=await this.$API.default.user.registerUser(userInfo);
          if(result==true){
            this.$message({
              type:'success',
              message:'注册成功',
              duration:2000,
            })
          }else{
            this.$message({
              type:'warning',
              message:result
            })
          }
          this.$router.push({path: '/login'});
        } else {
          this.$message({
            type:'warning',
            message:'信息错误',
            duration:2000
          })
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
}
</script>

<style scoped>
.login-body {
  /*background: #42b983 ;*/
  background: #65cea7 url("~@/assets/images/csgo.jpg");
  background-size: cover;
  padding-top: 1px;
  width: 100%;
  height: 100vh;
}
.container {
  margin-right: auto;
  margin-left: auto;
  padding-left: 15px;
  padding-right: 15px
}

@media (min-width: 768px) {
  .container {
    width: 750px
  }
}

@media (min-width: 992px) {
  .container {
    width: 970px
  }
}

@media (min-width: 1200px) {
  .container {
    width: 1170px
  }
}
.form-signin {
  max-width: 330px;
  margin: 100px auto;
  /*background: #29d;*/
  background: #fff;
  border-radius: 5px;
  -webkit-border-radius: 5px;
}

.form-signin .form-signin-heading {
  margin: 0;
  padding: 10px 15px;
  text-align: center;
  color: #fff;
  position: relative;
}

.sign-title {
  font-size: 24px;
  color: #fff;
  position: absolute;
  top: -60px;
  left: 0;
  text-align: center;
  width: 100%;
  text-transform: uppercase;
}


.form-signin .checkbox a, .form-signin .checkbox a:hover {
  color: #fff;
}


.form-signin input[type="text"], .form-signin input[type="password"] {
  margin-bottom: 15px;
  border-radius: 5px;
  -webkit-border-radius: 5px;
  border: 1px solid #eaeaec;
  background: #eaeaec;
  box-shadow: none;
  font-size: 12px;
}

.form-signin .btn-login {
  background: #6bc5a4;
  color: #fff;
  text-transform: uppercase;
  font-weight: normal;
  font-family: 'Open Sans', sans-serif;
  margin: 20px 0 5px;
  padding: 5px;
  -webkit-transition: all 0.3s;
  -moz-transition: all 0.3s;
  transition: all 0.3s;
  font-size: 30px;
}

.form-signin .btn-login:hover {
  background: #2ee09c;
  -webkit-transition: all 0.3s;
  -moz-transition: all 0.3s;
  transition: all 0.3s;
}

.form-signin p {
  text-align: left;
  color: #b6b6b6;
  font-size: 16px;
  font-weight: normal;
}

.form-signin a, .form-signin a:hover {
  color: #6bc5a4;
}

.form-signin a:hover {
  text-decoration: underline;
}

.login-wrap {
  padding: 15px 25px;
  position: relative;
}
.btn {
  display: inline-block;
  margin-bottom: 0;
  font-weight: 400;
  text-align: center;
  vertical-align: middle;
  cursor: pointer;
  background-image: none;
  border: 1px solid transparent;
  white-space: nowrap;
  padding: 6px 12px;
  font-size: 14px;
  line-height: 1.42857143;
  border-radius: 4px;
  -webkit-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  user-select: none
}
.btn-lg, .btn-group-lg > .btn {
  padding: 10px 16px;
  font-size: 18px;
  line-height: 1.33;
  border-radius: 6px
}
.btn-block {
  display: block;
  width: 100%;
  padding-left: 0;
  padding-right: 0
}

.btn:hover, .btn:focus {
  color: #333;
  text-decoration: none
}

.registration {
  color: #c7c7c7;
  text-align: center;
  margin-top: 15px;
}
</style>